<template>
	<view class="withdraw par-bg">
		<view class="content">
      <view class="content-title"><text style="font-size: 28rpx;">¥</text>{{userInfo.balance}}</view>
			<view class="pay-ipt">
				<text>提现金额</text>
				<input  type="number" v-model="withdraw_money"  :placeholder="'本次最多可转' + userInfo.balance + '元'" />
				<view class="all-btn" @click="getAll">全部</view>
				<view class="sign">¥</view>
			</view>
      <view class="pay-ipt" v-if="withdraw_array.length > 0">
      	<text>提现方式</text>
       <picker style="flex: 1"  @change="withdrawChange" :range="withdraw_array" :range-key="'withdraw_type'">
         <text>{{ withdraw_value.withdraw_type || '请选择提现方式' }}{{withdraw_name || ''}}</text>
       </picker>
      	<view class="all-btn" @click="chooseType">查看</view>
      </view>
      <view v-if="withdraw_array.length > 0" class="to-withdraw active" @tap="toWithdraw">提交</view>
			<view v-else class="to-withdraw active" @tap="chooseType">去添加提现方式</view>
      
		</view>
		<view class="tips">
			<view class="title">提现说明：</view>
			<view class="text">1.提现申请时间为每周{{returnWeek(week)}}</view>
			<view class="text">2.提现申请审核完成后才可进行下一笔申请</view>
      <view class="text" v-if="fee">3.提现将收取金额的{{parseInt(fee) + 6}}%作为手续费（包含{{fee}}%营业税+税费6%代扣所得税”</view>
		</view>
		<view class="line"></view>
		<view class="des">提现记录</view>
		<view class="withdraw-content" v-if="withdrawList.length > 0">
			<view class="record-item" v-for="(item, index) in withdrawList" :key="index">
				<view class="left">
					<view class="title success" v-if="item.status == 1">申请中</view>
					<view class="title success" v-if="item.status == 2">提现成功</view>
					<view class="title fail" v-if="item.status === 3">提现失败({{item.refuse_text}})</view>
					<view class="time">{{item._add_time_str}}</view>
				</view>
				<view :class="item.status == 2 ? 'right success' : 'right'">
					{{item.withdraw_money}}
				</view>
			</view>
		</view>
		<view v-if="withdrawList.length === 0" style="text-align:center;margin-top: 10rpx;font-size: 22rpx;color: #c3c3c3">暂无提现记录</view>
	</view>
</template>

<script>
// import { cardtest } from '../../util/utils'
	export default {
		data() {
			return {
				withdraw_money: null,
				withdrawList: [],
				userInfo: {},
				page: 1,
				pageSize: 100,
        withdraw_value: {},
        withdraw_name: '',
        withdraw_array: [],
        fee: '',
        week: ''
			}
		},
		onShow() {
			this.userInfo =  vk.getVuex('$user.userInfo')
			if(this.userInfo._id) {
				this.getWithdrawList()
        this.getWithdrawTypeList()
        this.getWithdrawSetList()
			}
		},
		methods: {
      returnWeek(week) {
        switch (week) {
          case '1':
            return '一'
          case '2':
            return '二'
          case '3':
            return '三'
          case '4':
            return '四'
          case '5':
            return '五'
          case '6':
            return '六'
          case '0':
            return '天'
        }
      },
      withdrawChange(e) {
        this.withdraw_value = this.withdraw_array[e.detail.value]
        if (this.withdraw_value.withdraw_value == 1) {
          this.withdraw_name = '(' + this.withdraw_value.wx_pay   +')'
        }
        if (this.withdraw_value.withdraw_value == 2) {
          this.withdraw_name = '(' + this.withdraw_value.ali_pay   +')'
        }
        if (this.withdraw_value.withdraw_value == 3) {
          this.withdraw_name = '(' + this.withdraw_value.card_num   +')'
        }
      },
      async getWithdrawSetList() {
        let info = await this.vk.callFunction({
        	url: 'user/kh/withdrawSetList',
        	title:'loading...',
        	data: {
            type: 1
          }
        })
        if(info.code == 0) {
        	this.fee = info.rows[0].fee;
          this.week = info.rows[0].week;
        }
      },
      async getWithdrawTypeList() {
        let data = {
        	pageNum: this.page,
        	pageSize: this.pageSize
        }
        data.user_id = this.userInfo._id
        let info = await this.vk.callFunction({
        	url: 'user/kh/withdrawTypeList',
        	title:'loading...',
        	data
        })
        if(info.code == 0) {
        	this.withdraw_array = info.rows;
        }
      },
			async getWithdrawList() {
					let data = {
						pageNum: this.page,
						pageSize: this.pageSize
					}
					data.user_id = this.userInfo._id
					let info = await this.vk.callFunction({
						url: 'user/kh/withdrawList',
						title:'loading...',
						data
					})
					if(info.code == 0) {
						this.withdrawList = info.rows;
					}
				},
			getAll() {
				this.withdraw_money = this.userInfo.balance;
			},
			async toWithdraw() {
				// 校验
				if (!this.withdraw_money || this.withdraw_money <= 0 ) {
					return uni.showToast({
						icon: 'none',
						title: '申请金额要大于0'
					})
				}
				if (this.withdraw_money > this.userInfo.balance) {
					return uni.showToast({
						icon: 'none',
						title: '申请金额要小于最大值'
					})
				}
        if (!this.withdraw_value._id) {
        	return uni.showToast({
        		icon: 'none',
        		title: '请选择提现方式'
        	})
        }
        let dateInfo = vk.pubfn.getDateInfo(new Date());
        if (dateInfo.week !== this.week) {
          return vk.toast('提现申请时间为每周' + this.returnWeek(this.week), 'none');
        }
				// 先判断是否有 未完成的提现
				let info = await this.vk.callFunction({
					url: 'user/kh/withdrawList',
					title:'loading...',
					data: {
						pageNum: 1,
						pageSize: 100,
						status: 1,
						user_id: this.userInfo._id
					}
				})
				if(info.code == 0) {
					if (info.rows.length > 0) {
						uni.showModal({
							title: '提示',
							content: '您已经提交了提现申请，审核后才可再申请',
							showCancel: false,
							success: (res) => {
							}
						})
					} else {
						// 申请提现
            let obj = {
              user_id: this.userInfo._id,
              username: this.userInfo.nickname,
              mobile: this.userInfo.mobile,
              withdraw_money: this.withdraw_money,
              status: 1,
              openid: this.userInfo.wx_openid["mp-weixin"]
            }
            let newObj = Object.assign({}, this.withdraw_value)
            newObj.withdraw_type_id = newObj._id
            delete newObj._id
            obj = Object.assign(obj, newObj)
						let info = await this.vk.callFunction({
							url: 'user/kh/withdrawPost',
							title:'loading...',
							data: obj
						})
						if(info.code == 0) {
							uni.showModal({
								title: '提示',
								content: '申请成功,审核通过后会微信服务消息通知您',
								showCancel: false,
								success: (res) => {
                  vk.userCenter.getCurrentUserInfo({
                    success: (data) => {
                      // 成功后的逻辑
                      vk.setVuex('$user.userInfo', data.userInfo)
                    }
                  });
                  wx.requestSubscribeMessage({tmplIds: ['L7rMKZBNmirvpJzr_N0qEJrZUoh_vBPs8Ub2jsZKT7k'],
                  complete:res=>{
                    uni.navigateBack()
                  },
                  })
                  
								}
							})
						}
					}
				}
			},
			chooseType() {
				uni.navigateTo({
				  url: '/pages/withdrawset/withdrawset'
				})
			}
		}
	}
</script>

<style lang="scss">
page {
	height: 100%!important;
}
.withdraw {
	height: 100%;
	.price-info {
		width: 100%;
		height: 378upx;
		position: relative;
		image {
			width: 100%;
		}
		.header {
			position: absolute;
			top: 0;
			font-size: 32upx;
			color: #FFFFFF;
			line-height: 45upx;
			overflow: hidden;
			display: flex;
			align-items: center;
			image {
				width: 28upx;
				height: 48upx;
				margin-left: 32upx;
				margin-right: 30upx;
			}
		}
		.text {
			position: absolute;
			left: 50%;
			transform: translateX(-50%);
			bottom: 90upx;
			font-size: 26upx;
			color: #FFFFFF;
			line-height: 38upx;
			text-align: center;
			.price {
				font-size: 66upx;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 98upx;
			}
		}
	}
	.content {
		width: 690upx;
		height: 560upx;
		background: #FFFFFF;
		box-shadow: 0upx 2upx 10upx 0upx rgba(178, 186, 191, 0.4);
		border-radius: 16upx;
		margin: 0 auto;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		align-items: center;
    &-title {
      font-size: 40rpx;
      padding-top: 32rpx;
      text-align: center;
      font-family: DIN;
      font-size: 70rpx;
      font-style: normal;
      font-weight: 500;
      color: #1D2129;
    }
		.pay-ipt {
			display: flex;
			align-items: center;
			border-bottom: 1px solid #979797;
			font-size: 40upx;
			color: #2B2B2B;
			line-height: 84upx;
			margin-top: 20upx;
       width: 634upx;
			position: relative;
			text {
				width: 200upx;
				font-size: 28upx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #515151;
			}
			input {
				flex: 1;
				font-size: 28upx;
				color: #333;
				line-height: 40upx;
			}
			.sign {
				position: absolute;
				left: 180upx;
				top: 50%;
				transform:  translateY(-50%);
				font-size: 30upx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #2B2B2B;
			}
		}
		.all-btn {
			font-size: 28upx;
			color: #005ECD;
			line-height: 40upx;
			white-space: nowrap;
		}
		.to-withdraw {
			width: 500upx;
			height: 80upx;
			background: #F9FAFB;
			border-radius: 40upx;
			text-align: center;
			line-height: 80upx;
			font-size: 32upx;
			color: #CECFCF;
			margin-top: 30upx;
			background: #005ECD;
			color: #FFFFFF;
		}
	}
	.tips {
		margin: 30upx 50upx;
		.title {
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			font-size: 32upx;
			color: #6f6f6f;
		}
		.text {
			font-size: 26upx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #555555;
		}
	}
	.line {
		background: #d1d1d1;
		height: 2upx;
		width: 100%;
		margin: 20upx 0;
	}
	.des {
		text-align: center;
		font-size: 32upx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #686868;
	}
	.withdraw-content {
		background: #fff;
		margin-top: 10upx;
		.record-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			width: 690upx;
			padding: 0 25upx;
			border-bottom: 1upx solid #AEAEAE;
			.left  {
				.title {
					font-size: 32upx;
					line-height: 45upx;
					color: #000000;
					margin: 24upx 0 14upx;
					&.success {
						color: #005ECD;
					}
				}
				.time {
					font-size: 24upx;
					color: #8E8F90;
					line-height: 33upx;
					margin-bottom: 20upx;
				}
			}
			.right {
				font-size: 40upx;
				font-weight: 500;
				color: #B3B3B3;
				line-height: 60upx;
				&.success {
					color: #005ECD;
				}
			}
		}
	}
}
</style>
